import axios from 'axios'
import nprogress from 'nprogress'
import store from '@/store'

var requests = axios.create({
    baseURL: '/api',
    timeout: 5000
});
//请求拦截器， 发送n次请求，每次请求时拦截器都会被触发
requests.interceptors.request.use((config)=> {
    // 携带头信息给服务器 给所有请求携带公共信息
    nprogress.start();
    if (store.state.shopCart.uuidToken) {
        config.headers.userTempId = store.state.shopCart.uuidToken;
    }
    if (store.state.user.token) {
        config.headers.token = store.state.user.token;
    }
    return config;
})
requests.interceptors.response.use((res)=> {
    nprogress.done();
    return res.data;
},(erro)=> {
    Promise.reject(erro);
})

export default requests;